{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from captcha.image import ImageCaptcha\n",
    "import numpy as np\n",
    "from PIL import Image\n",
    "import random\n",
    "import sys"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "生成完毕验证码，10000 / 10000\n"
     ]
    }
   ],
   "source": [
    "number = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n",
    "\n",
    "def random_captcha_text(num, char_set=number, captcha_size=4):\n",
    "    \n",
    "    captcha_text = []\n",
    "    numbers = \"%04d\" % num\n",
    "    for i in range(captcha_size):\n",
    "        \n",
    "        c = numbers[i]\n",
    "        captcha_text.append(c)\n",
    "    \n",
    "    return captcha_text\n",
    "\n",
    "def gen_captcha_text_and_image(num):\n",
    "    \n",
    "    image = ImageCaptcha()\n",
    "    \n",
    "    captcha_text = random_captcha_text(num=num)\n",
    "    \n",
    "    captcha_text = \"\".join(captcha_text)\n",
    "    \n",
    "    captcha = image.generate(captcha_text)\n",
    "    \n",
    "    image.write(captcha_text, \"c:/learn/captcha/images/\" + captcha_text + \".jpg\")\n",
    "\n",
    "num = 10000\n",
    "if __name__ == \"__main__\":\n",
    "    for i in range(num):\n",
    "        gen_captcha_text_and_image(i)\n",
    "        print(\"正在生成验证码，%d / 10000\" %(i+1), end=\"\\r\")\n",
    "        \n",
    "    print(\"\\n生成完毕\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
